<?php

namespace App\Http\Controllers\Admin;

use App\Admin\Photoalbum;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class PhotoalbumController extends Controller
{
    //
    public function column(){
        $data =  DB::table('photoalbum')
            ->join('manager','manager.id','=','photoalbum.user_id')
            ->select('photoalbum.*','manager.name')
            ->where('photoalbum.status','<>',2)
            ->paginate(10);

        return view('admin.photoalbum.column',compact('data'));
    }
    public function stop(Request $request){
        $data =  $request->route( 'id' );
        Photoalbum::where('id',$data)->update(['status'=>0]);
        return json_encode(['message'=>'禁用成功']);
    }
    public function start(Request $request){
        $data =  $request->route( 'id' );
        Photoalbum::where('id',$data)->update(['status'=>1]);
        return json_encode(['message'=>'启用成功']);
    }
    public function del(Request $request){
        $data =  $request->route( 'id' );
        Photoalbum::where('id',$data)->update(['status'=>2]);
        return json_encode(['message'=>'删除成功']);
    }
    public function edit(Request $request){
        $data =  $request->route( 'id' );
        $data =  Photoalbum::where('id',$data)->get();
        return view('admin/photoalbum/edit',compact('data'));
    }
    public function edit_up( Request $request){
        if ($request->isMethod('post')){
            $data = $request->all();
            unset($data['_token']);
            DB::table('photoalbum')->where('id',$data['id'])->update([
                'sort'=>$data['sort'],
                'cate_name'=>$data['cate_name'],
            ]);
            return json_encode(['message'=>'修改成功']);
        }
    }
    public function add_up(Request $request){
        if ($request->isMethod('post')){

            $data = $request->all();
            unset($data['_token']);
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['user_id'] = Auth::guard('admin') -> user()->id;
            DB::table('photoalbum')->insert($data);
            return json_encode(['message'=>'添加成功']);
        }
    }
}
